/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/
package world.ah.wf.guiclient;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class StringTable {
  private int columnCount;
  public int getColumnCount() {return columnCount;}
  //
  private ArrayList<String[]> data = new ArrayList<String[]>();
  //
  public int getRowCount() {return data.size();}
  //
  public StringTable(int columnCount) {this.columnCount = columnCount;};
  //
  public void add() {
    String[] strings = new String[columnCount];
    for(int i = 0; i < columnCount; i++) strings[i] = "";
    data.add(strings);
  };
  //
  public void set(int row, int column, String value) {((String[])(data.get(row)))[column] = value;}
  //
  public String get(int row, int column) {return ((String[])(data.get(row)))[column];}
  //
  public void clear() {data.clear();}
  //
  public String[][] toStringArray() {
    String[][] result = new String[data.size()][columnCount];
    for(int row = 0; row < data.size(); row++) for(int col = 0; col < columnCount; col++) 
      result[row][col] = ((String[])(data.get(row)))[col];
    return result;
  }
  //
  public int getRowOf(int column, String value) {
    for(int row = 0; row < data.size(); row++) 
      if(((String[])(data.get(row)))[column].equals(value)) return row;
    return -1;
  }
  //
  public void store(OutputStream out) throws IOException {
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(out, "UTF-16BE"));
    for(int row = 0; row < data.size(); row++) {
      if(row > 0) wr.write('\n');
      for(int col = 0; col < columnCount; col++) {
        String s = ((String[])(data.get(row)))[col];
        wr.write((col > 0 ? ";" : "") + "\"" + s.replaceAll("\"", "\"\"") + "\"");
      }
    }
    wr.flush(); 
  }
  //
  public void load(InputStream inStream) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "UTF-16BE"));
    int st = 0;
    int col = 0;
    String val = "";
    while(true) {
      int i = in.read();
      if(i < 0) break;
      char c = (char)i;
      //
      if(st == 0) {
        if(c == '\"') {if(col == 0) add();  st = 1;}
        else if(c == '\r') {}
        else if(c == '\n') {}
        else if(Character.isSpaceChar(c)) {}
        else throw new Exception("wrong csv");
      } else if(st == 1) {
        if(c == '\"') st = 2;
        else val += c;
      } else if(st == 2) {
        if(c == '\"') {val += c; st = 1;}
        else {
          set(data.size() - 1, col, val); 
          val = ""; 
          if(c == ';') {col++; st = 0;}
          else if(c == '\n') {col = 0; st = 0;}
          else if(c == '\r') st = 3;
          else if(Character.isSpaceChar(c)) st = 3;
          else throw new Exception("wrong csv");
        }
      } else if(st == 3) {
        if(c == ';') {col++; st = 0;}
        else if(c == '\n') {col = 0; st = 0;}
        else if(c == '\r') {}
        else if(Character.isSpaceChar(c)) {}
        else throw new Exception("wrong csv");
      }
    }
    if(st == 2) set(data.size() - 1, col, val);
    else if(st == 1) throw new Exception("wrong csv");
  }
}
